package com.eddie.jdbc.service.impl;

import com.eddie.jdbc.dao.impl.BaseDaoImpl;
import com.eddie.jdbc.entity.AccountEntity;
import com.eddie.jdbc.service.AccountService;
import com.eddie.jdbc.vo.AccountVo;
import com.eddie.jdbc.vo.params.FindAccountParams;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author Eddie
 */
public class AccountServiceImpl extends BaseDaoImpl<AccountEntity> implements AccountService {


    @SneakyThrows
    @Override
    public AccountVo findByUserNameAndPassword(FindAccountParams params) {
        String sql = "select *from %s where username=? AND password = ?".formatted(simpleName);
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, params.getUsername());
        ps.setObject(2, params.getPassword());
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            AccountEntity account = new AccountEntity();
            getFilelds(rs, account);
            AccountVo accountVo = new AccountVo();
            BeanUtils.copyProperties(account,accountVo);
            return accountVo;
        }
        return null;
    }

}
